<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
<!--      <el-form-item label="分类类别" prop="categoryId">-->
<!--        <el-select-->
<!--          v-model="queryParams.categoryId"-->
<!--          placeholder="请选择分类类别"-->
<!--          clearable-->
<!--          class="!w-240px"-->
<!--        >-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="lastUpdateTime">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.lastUpdateTime"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          v-model="queryParams.publishTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="标签" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入标签"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

<!--      <el-form-item label="发布状态" prop="publish">-->
<!--        <el-input-->
<!--          v-model="queryParams.publish"-->
<!--          placeholder="请输入发布状态"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="置顶状态" prop="topping">-->
<!--        <el-input-->
<!--          v-model="queryParams.topping"-->
<!--          placeholder="请输入置顶状态"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="来源" prop="source">-->
<!--        <el-input-->
<!--          v-model="queryParams.source"-->
<!--          placeholder="请输入来源"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cms:article:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cms:article:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" border :stripe="true">
      <el-table-column label="封面图" align="center" width="110">
        <template #default="scope">
          <el-image
            v-if="scope.row.coverImg"
            class="h-50px w-80px"
            lazy
            :src="scope.row.coverImg"
            :preview-src-list="[scope.row.coverImg]"
            preview-teleported
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="栏目" align="center" prop="categoryName" min-width="90" />
      <el-table-column label="标题" align="center" prop="title" min-width="300" />
      <el-table-column label="点击量" align="center" prop="views" width="60" />
      <el-table-column label="描述" align="center" prop="description"  min-width="300"  :show-overflow-tooltip="true"/>
      <el-table-column label="标签" align="center" prop="tags" min-width="160" />
      <el-table-column label="链接地址" align="center" prop="linkUrl" min-width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="来源" align="center" prop="source" min-width="160" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="140"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="140"
      />
      <el-table-column label="有效状态" align="center" prop="publish" width="65" fixed="right">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="发布状态" align="center" prop="topping" width="65" fixed="right">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.publish" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="70">
        <template #default="scope">
          <div>
          <el-button
            plain
            size="small"
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:article:update']"
          >
            编辑
          </el-button>
          </div>
          <div class="mt-2">
          <el-button
            plain
            size="small"
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:article:delete']"
          >
            删除
          </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ArticleForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getStrDictOptions, getDictOptions, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ArticleApi, ArticleVO } from '@/api/cms/article'
import ArticleForm from './ArticleForm.vue'

/** 文章 列表 */
defineOptions({ name: 'Article' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ArticleVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  lastUpdateTime: [],
  title: undefined,
  description: undefined,
  tags: undefined,
  categoryId: undefined,
  categoryKey: undefined,
  publish: undefined,
  topping: undefined,
  publishTime: [],
  coverImg: undefined,
  delFlag: undefined,
  views: undefined,
  supportSign: undefined,
  source: undefined,
  publishUser: undefined,
  linkUrl: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ArticleApi.getArticlePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ArticleApi.deleteArticle(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ArticleApi.exportArticle(queryParams)
    download.excel(data, '文章.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped>
:deep(.el-table) {
  position: relative;
  overflow: hidden;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  width: 100%;
  max-width: 100%;
  font-size: 12px;
  color: #000000;
}
:deep(.REFUND-row) {
  background-color: #F3D39BFF !important;
}

:deep(.fails-row) {
  background-color: #D15B47 !important;
}

:deep(.success-row) {
  background-color: #36bc99 !important;
}
:deep(.my-table td) {
  padding: 5px 0 !important;
}

:deep(.el-table--default .cell) {
  padding: 0 6px !important;
}

</style>
